package com.hcinfos.gsoo.ui;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chatuidemo.activity.MainActivity;
import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.enity.User;
import com.hcinfos.gsoo.util.SharePreferenceUtil;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

public class BaseMenuActivity extends SlidingFragmentActivity implements
		OnClickListener {

	private SharePreferenceUtil util;
	public TextView login_tv;

	public static String UPDATE_ACTION = "com.login.update.action";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setBehindContentView(R.layout.frame_menu);

		util = new SharePreferenceUtil(this, "setting");
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.shadow_width);
		// sm.setShadowDrawable(R.drawable.shadow);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		initRes();

	}

	@Override
	protected void onPause() {
		unregisterReceiver(receiver);
		super.onPause();
	};

	BroadcastReceiver receiver = new BroadcastReceiver() {

		public void onReceive(android.content.Context context, Intent intent) {
			login_tv.setText("未登陆");
			isLogin = false;
		};
	};

	private RelativeLayout login_rl;
	private RelativeLayout city_rl;
	private RelativeLayout im_rl;
	private RelativeLayout setting_rl;
	private RelativeLayout add_rl;
	private RelativeLayout search_rl;
	private RelativeLayout gift_rl;
	private RelativeLayout choujiang_rl;
	private RelativeLayout home_rl;
	private RelativeLayout lp_rl;

	private void initRes() {

		home_rl = (RelativeLayout) findViewById(R.id.home_rl);
		home_rl.setOnClickListener(this);
		login_tv = (TextView) findViewById(R.id.login_tv);
		login_rl = (RelativeLayout) findViewById(R.id.login_rl);
		login_rl.setOnClickListener(this);
		city_rl = (RelativeLayout) findViewById(R.id.city_rl);
		city_rl.setOnClickListener(this);
		im_rl = (RelativeLayout) findViewById(R.id.im_rl);
		im_rl.setOnClickListener(this);
		setting_rl = (RelativeLayout) findViewById(R.id.setting_rl);
		setting_rl.setOnClickListener(this);
		add_rl = (RelativeLayout) findViewById(R.id.add_rl);
		add_rl.setOnClickListener(this);
		search_rl = (RelativeLayout) findViewById(R.id.search_rl);
		search_rl.setOnClickListener(this);
		gift_rl = (RelativeLayout) findViewById(R.id.gift_rl);
		gift_rl.setOnClickListener(this);
		choujiang_rl = (RelativeLayout) findViewById(R.id.choujiang_rl);
		choujiang_rl.setOnClickListener(this);
		lp_rl = (RelativeLayout) findViewById(R.id.lp_rl);
		lp_rl.setOnClickListener(this);
	}

	private boolean isLogin = false;

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		IntentFilter filter = new IntentFilter(UPDATE_ACTION);
		registerReceiver(receiver, filter);
		User user = (User) util.loadObject("user");
		if (user != null) {
			login_tv.setText("个人中心");
			login_tv.setText(user.getGgnumber());
			isLogin = true;
		} else {
			login_tv.setText("未登陆");
			isLogin = false;
		}
	}

	private Intent intent;
	private boolean isHome = true;

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.home_rl:
			lp_rl.setSelected(false);

			home_rl.setSelected(true);
			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			choujiang_rl.setSelected(false);
			break;
		case R.id.login_rl:
			lp_rl.setSelected(false);

			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			choujiang_rl.setSelected(false);
			home_rl.setSelected(false);
			if (!isLogin) {
				intent = new Intent(this, LoginActivity.class);
				startActivity(intent);
			} else {
				intent = new Intent(this, MyDetailActivity.class);
				startActivity(intent);
			}
			break;
		case R.id.city_rl:
			login_rl.setSelected(false);
			city_rl.setSelected(true);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			choujiang_rl.setSelected(false);
			home_rl.setSelected(false);
			lp_rl.setSelected(false);

			intent = new Intent(this, CityListActivity.class);
			startActivity(intent);
			finish();
			break;
		case R.id.setting_rl:
			lp_rl.setSelected(false);

			login_rl.setSelected(false);
			city_rl.setSelected(false);
			setting_rl.setSelected(true);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			choujiang_rl.setSelected(false);
			intent = new Intent(this, SettingActivity.class);
			startActivity(intent);
			home_rl.setSelected(false);

			break;
		case R.id.add_rl:
			lp_rl.setSelected(false);

			login_rl.setSelected(false);
			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(true);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			choujiang_rl.setSelected(false);
			home_rl.setSelected(false);

			intent = new Intent(this, AddShopActivity.class);
			startActivity(intent);
			break;
		case R.id.gift_rl:
			lp_rl.setSelected(false);

			login_rl.setSelected(false);
			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(true);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			choujiang_rl.setSelected(false);
			home_rl.setSelected(false);
			isHome = false;
			intent = new Intent(this, RandomNearActivity.class);
			startActivity(intent);
			break;
		case R.id.search_rl:
			choujiang_rl.setSelected(false);
			login_rl.setSelected(false);
			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(true);
			im_rl.setSelected(false);
			intent = new Intent(this, SearchActivity.class);
			startActivity(intent);
			home_rl.setSelected(false);
			lp_rl.setSelected(false);

			break;
		case R.id.im_rl:
			lp_rl.setSelected(false);

			choujiang_rl.setSelected(false);
			login_rl.setSelected(false);
			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(true);
			home_rl.setSelected(false);

			if (util.loadObject("user") != null) {
				intent = new Intent(this, MainActivity.class);
			} else {
				intent = new Intent(this, LoginActivity.class);
			}
			startActivity(intent);
			overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
			break;
		case R.id.choujiang_rl:
			lp_rl.setSelected(false);
			choujiang_rl.setSelected(true);
			choujiang_rl.setSelected(false);
			login_rl.setSelected(false);
			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			home_rl.setSelected(false);
			intent = new Intent(this, ChoujiangActivity.class);
			startActivity(intent);
			break;
		case R.id.lp_rl:
			lp_rl.setSelected(true);
			choujiang_rl.setSelected(false);
			choujiang_rl.setSelected(false);
			login_rl.setSelected(false);
			city_rl.setSelected(false);
			setting_rl.setSelected(false);
			add_rl.setSelected(false);
			gift_rl.setSelected(false);
			search_rl.setSelected(false);
			im_rl.setSelected(false);
			home_rl.setSelected(false);
			intent = new Intent(this, GiftActivity.class);
			startActivity(intent);
			break;
		}
	}

}
